package com.k2data.k2app.service.pas14;


import com.alibaba.fastjson.JSON;
import com.k2data.k2app.common.K2Assert;
import com.k2data.k2app.domain.pas14.project.Project;
import com.k2data.k2app.domain.pas14.project.ProjectDataSet;
import com.k2data.k2app.domain.pas14.project.ProjectFlow;
import com.k2data.k2app.domain.pas14.project.ProjectScriptAlg;
import com.k2data.k2app.rest.domain.RectObject;
import com.k2data.k2app.util.JsonUtils;
import com.k2data.k2app.util.PasHttpUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从pas取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class Pas14ProjectService extends Pas14Basic {

	/**
	 * 复制项目
	 * @param url
	 * @param projectId
	 * @param creator
	 * @param newProjName
	 * @return
	 * @throws Exception
	 */

	/**
	 *  复制项目
	 *  <br>若开启sso,传递的creator与从登录信息获取的不同,报USER_ILLEGAL
	 * @param projectId 项目id
	 * @param newProjName 项目名
	 * @return
	 * @throws Exception
	 */
	public Project copy(Long projectId,String newProjName) throws Exception {
		String url = getProjectUrl()+"/copy?projId="+ projectId + "&creator="+getCreator() + "&newProjName=" + newProjName;
		String infoError = PasHttpUtil.onGet(url);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		RectObject object = (RectObject)JsonUtils.JSONToObj(infoError, RectObject.class);
		String result = JSON.toJSONString(object.getResult());
		Project project = (Project)JsonUtils.JSONToObj(result, Project.class);
		return project;
	}

	/**
	 * 删除项目
	 * @param projectId 项目id
	 * @return 被删除项目 id
	 * @throws Exception
	 */
	public String delete(Long projectId) throws Exception{
		String infoError = PasHttpUtil.onDelete(getProjectUrl()+"/"+projectId);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		String id= resultMap.get("result") == null?"":resultMap.get("result").toString();
		return id;
	}

	/**
	 * 运行项目
	 * @param projectId 项目id
	 * @throws Exception
	 */
	public String run(Long projectId) throws Exception {
		Map<String,String> param = new HashMap<>(16);
		param.put("mqSuffix",getMqSuffix());
		String infoError = PasHttpUtil.onPost(getRunJobUrl(projectId),param);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		Map<String,Object> jobIdMap = JsonUtils.convertJsonStrToMap(resultMap.get("result"));
		String jobId = jobIdMap.get("jobId").toString();
		return jobId;
	}

	/**
	 * 计算节点信息＜script-alg＞
	 * @param projId 模型id
	 * @param nodeId 节点id
	 * @return 节点信息
	 * @throws Exception
	 */
	public ProjectScriptAlg getScriptAlg(Long projId, String nodeId) throws Exception {
		String url = getProjectUrl() + "/script-alg?projId="+projId+"&id="+nodeId;
		String infoError = PasHttpUtil.onGet(url);
		RectObject object = (RectObject)JsonUtils.JSONToObj(infoError, RectObject.class);
		K2Assert.isExceptionByPas(object);
		String result = JSON.toJSONString(object.getResult());
		ProjectScriptAlg projectScriptAlg = (ProjectScriptAlg)JsonUtils.JSONToObj(result, ProjectScriptAlg.class);
		return projectScriptAlg;
	}

	/**
	 * 查询数据源节点信息
	 * @param projId 模型id
	 * @param nodeId 节点id
	 * @return 节点信息
	 * @throws Exception
	 */
	public ProjectDataSet getDataSet(Long projId, String nodeId) throws Exception {
		String url = getProjectUrl() + "/data-set?projId="+projId+"&id="+nodeId;
		String infoError = PasHttpUtil.onGet(url);
		RectObject object = (RectObject)JsonUtils.JSONToObj(infoError, RectObject.class);
		K2Assert.isExceptionByPas(object);
		String result = JSON.toJSONString(object.getResult());
		ProjectDataSet projectDataSet = (ProjectDataSet)JsonUtils.JSONToObj(result, ProjectDataSet.class);
		return projectDataSet;
	}

	/**
	 * 编辑数据源节点信息
	 * @param projectDataSet 节点信息
	 * @return 节点id
	 */
	public String editDataSet(ProjectDataSet projectDataSet) throws Exception {
		String url = getProjectUrl() + "/data-set";
		String infoError = PasHttpUtil.onPutObject(url, projectDataSet);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		String id= resultMap.get("result") == null?"":resultMap.get("result").toString();
		return id;
	}

	/**
	 * 编辑计算节点信息
	 * @param projectScriptAlg 计算节点信息
	 * @return 节点id
	 */
	public String editScriptAlg(ProjectScriptAlg projectScriptAlg) throws Exception {
		String url = getProjectUrl() + "/script-alg";
		String infoError = PasHttpUtil.onPutObject(url, projectScriptAlg);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		String id= resultMap.get("result") == null?"":resultMap.get("result").toString();
		return id;
	}
	/**
	 * 根据id查询项目
	 * json字符串封装为project对象
	 * @param projectId
	 * @return
	 * @throws Exception
	 */
	public Project getBasic(Long projectId) throws Exception {
		String url = getProjectUrl() + "/" +projectId;
		String infoError = PasHttpUtil.onGet(url);
		RectObject object = (RectObject)JsonUtils.JSONToObj(infoError, RectObject.class);
		String result = JSON.toJSONString(object.getResult());
		Project project = (Project)JsonUtils.JSONToObj(result, Project.class);
		return project;
	}

	/**
	 * 根据id查询项目flow
	 * json字符串封装为project对象
	 * @param projectId
	 * @return
	 * @throws Exception
	 */
	public ProjectFlow getFlow(Long projectId) throws Exception {
		String url = getProjectUrl() + "/flow/" +projectId;
		String infoError = PasHttpUtil.onGet(url);
		RectObject object = (RectObject)JsonUtils.JSONToObj(infoError, RectObject.class);
		String result = JSON.toJSONString(object.getResult());
		ProjectFlow project = (ProjectFlow)JsonUtils.JSONToObj(result, ProjectFlow.class);
		return project;
	}

	/**
	 *  查询所有项目
	 *  封装为project对象
	 *  @param map 查询条件，可以是分页page size owner
	 * @return list
	 * @throws Exception
	 */
	public List<Project> listBasic(Map map) throws Exception {
		String condition = getQueryCondition(map);
		String url = getProjectUrl() + "?" + condition;
		List<Project> projects = new ArrayList<>();
		String infoError = PasHttpUtil.onGet(url);
		Map object = JsonUtils.convertJsonStrToMap(infoError);
		List list = JsonUtils.convertJsonStrToList(object.get("result"));
		for(int i = 0;i < list.size(); i++){
			Project project = (Project)JsonUtils.JSONToObj(list.get(i).toString(),Project.class);
			projects.add(project);
		}
		return projects;
	}

	/**
	 * 编辑项目
	 * @param project
	 * @return
	 * @throws Exception
	 */
	public Project editBasic(Project project) throws Exception {
		project.setIconUrl(null);
		String url = getProjectUrl();
		String infoError = PasHttpUtil.onPutObject(url,project);
		Map<String, Object> resultMap = JsonUtils.convertJsonStrToMap(infoError);
		K2Assert.isExceptionByPas(resultMap);
		return project;
	}
}
